<?php
class ShooterService extends BaseService
{

    private $redisObj;
    static private $_instance = NULL;

    private function  __construct()
    {
        $this->redisObj = Config::getRedisClient();
    }

    private function  __clone()
    {
    }

    static public function getInstance()
    {
        if (is_null(self::$_instance) || !isset(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    //email valid time
    public function genEmailExpireKey($emailId)
    {
        return "game:email:expire:".$emailId;
    }
    public function setEmailExpire($emailId)
    {
        $key = $this->genEmailExpireKey($emailId);
        $this->redisObj->set($key, "1");
        $this->redisObj->expire($key,3600*24*3);
    }
    public function isEmailValid($emailId)
    {
        $result = true;
        $key = $this->genEmailExpireKey($emailId);
        if (!$this->redisObj->exists($key))
        {
            $result = false;
        }
        return $result;
    }

    public function getTimeLeftOfEmail($emailId)
    {
        $key = $this->genEmailExpireKey($emailId);
        return $this->redisObj->ttl($key);
    }

    //setting
    public function  genSettingVersion()
    {
        return "game:setting:version";
    }

    public function setSettingVersion($version)
    {
        $key = $this->genSettingVersion();
        $this->redisObj->set($key, $version);
    }

    public function getSettingVersion()
    {
        $key = $this->genSettingVersion();
        return $this->redisObj->get($key);
    }

    public function  genSettingKey($key)
    {
        return "game:setting:" . $key;
    }

    public function getSettingByKey($key)
    {
        $keyT = $this->genSettingKey($key);
        return $this->redisObj->get($keyT);
    }

    public function setSettingByKey($key, $content)
    {
        $keyT = $this->genSettingKey($key);
        $this->redisObj->set($keyT, $content);
    }

    public function isNewVersionAvalible()
    {
        $result = false;
        if ($this->getSettingVersion() != Config::$settingVerison) {
            $result = true;
        }
        return $result;
    }

    public function updateSettingCache()
    {
        $settingArray = Config::getSettingFile();
        if (is_array($settingArray)) {
            foreach ($settingArray as $key => $fileLocation) {
                $content = file_get_contents($fileLocation);
                $content = str_replace(array("\n","\r"),'',$content);
                $this->setSettingByKey($key, $content);
            }
        }
    }

    public function getSettingData($key)
    {
        $result = array();
        if (strtolower($key)  == "all") {
            $settingArray = Config::getSettingFile();
            foreach ($settingArray as $key => $fileLocation) {
                $content = $this->getSettingByKey($key);
                if (!isset($content) || empty($content)) {
                    $content = file_get_contents($fileLocation);
                    $content = str_replace(array("\n","\r"),'',$content);
                    $this->setSettingByKey($key, $content);
                }
                $temp = array($key =>$content);
                $result = array_merge($result,$temp);
            }
        } else {
            $content = $this->getSettingByKey($key);
            if (!isset($content) || empty($content)) {
                $settingArray = Config::getSettingFile();
                if (array_key_exists($key, $settingArray)) {
                    $fileLocation = $settingArray[$key];
                    $content = file_get_contents($fileLocation);
                    $content = str_replace(array("\n","\r"),'',$content);
                    $this->setSettingByKey($key, $content);
                } else {
                    $content = "";
                }
            }
            $result = array($key => $content);
        }
        return $result;
    }

    public function verifyReceipt($receipt)
    {
        $data = array("receipt-data"=>$receipt);
        parent::llog(json_encode($data), "VerifyReceipt.sendData");
        $result = Trans::sendUrlPostJson(Config::$IAPVerify_url,json_encode($data));
        parent::llog($result, "VerifyReceipt.returnData");
        if(isset($result))
        {
            $result = json_decode($result);
            if($result->status == 0)
            {
                return $result;
            }else
            {
                parent::llog(json_encode($data), "VerifyReceipt.sendData1");
                $result = Trans::sendUrlPostJson(Config::$IAPVerify_url_sb,json_encode($data));
                parent::llog($result, "VerifyReceipt.returnData1");
                if(isset($result))
                {
                    return json_decode($result);
                }else
                {
                    return null;
                }
            }
        }else
        {
            parent::llog(json_encode($data), "VerifyReceipt.sendData2");
            $result = Trans::sendUrlPostJson(Config::$IAPVerify_url_sb,json_encode($data));
            parent::llog($result, "VerifyReceipt.returnData2");
            if(isset($result))
            {
                return json_decode($result);
            }else
            {
                return null;
            }
        }
    }

}